package com.example.casper.Experiment2024;


import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.longClick;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withParent;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static com.example.casper.Experiment2024.Test.ImageUtils.areImagesSame;
import static com.example.casper.Experiment2024.Test.ImageUtils.getCoverBitmap;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertTrue;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.ViewInteraction;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.GrantPermissionRule;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class Main2ActivityTest {

    @Rule
    public ActivityScenarioRule<Main2Activity> mActivityScenarioRule =
            new ActivityScenarioRule<>(Main2Activity.class);

    @Rule
    public GrantPermissionRule mGrantPermissionRule =
            GrantPermissionRule.grant(
                    "android.permission.ACCESS_FINE_LOCATION",
                    "android.permission.ACCESS_COARSE_LOCATION");

    @Test
    public void main2ActivityTest() {
        ViewInteraction bottomNavigationItemView = onView(
                allOf(withId(R.id.footprint), withContentDescription("足迹"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.bottom_navigation),
                                        0),
                                2),
                        isDisplayed()));
        bottomNavigationItemView.perform(click());

        ViewInteraction bottomNavigationItemView2 = onView(
                allOf(withId(R.id.tourmenory), withContentDescription("旅行记录"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.bottom_navigation),
                                        0),
                                1),
                        isDisplayed()));
        bottomNavigationItemView2.perform(click());

        ViewInteraction recyclerView = onView(
                allOf(withId(R.id.tour_records_recyclerView),
                        childAtPosition(
                                withClassName(is("android.widget.LinearLayout")),
                                0)));
        recyclerView.perform(actionOnItemAtPosition(0, longClick()));

        ViewInteraction materialTextView = onView(
                allOf(withId(android.R.id.title), withText("删除0"),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                0),
                        isDisplayed()));
        materialTextView.perform(click());

        ViewInteraction materialButton = onView(
                allOf(withId(android.R.id.button1), withText("确定"),
                        childAtPosition(
                                childAtPosition(
                                        withClassName(is("android.widget.ScrollView")),
                                        0),
                                3)));
        materialButton.perform(scrollTo(), click());

        ViewInteraction recyclerView2 = onView(
                allOf(withId(R.id.tour_records_recyclerView),
                        childAtPosition(
                                withClassName(is("android.widget.LinearLayout")),
                                0)));
        recyclerView2.perform(actionOnItemAtPosition(0, longClick()));

        ViewInteraction materialTextView2 = onView(
                allOf(withId(android.R.id.title), withText("删除0"),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                0),
                        isDisplayed()));
        materialTextView2.perform(click());

        ViewInteraction materialButton2 = onView(
                allOf(withId(android.R.id.button1), withText("确定"),
                        childAtPosition(
                                childAtPosition(
                                        withClassName(is("android.widget.ScrollView")),
                                        0),
                                3)));
        materialButton2.perform(scrollTo(), click());

        ViewInteraction recyclerView3 = onView(
                allOf(withId(R.id.tour_records_recyclerView),
                        childAtPosition(
                                withClassName(is("android.widget.LinearLayout")),
                                0)));
        recyclerView3.perform(actionOnItemAtPosition(0, longClick()));

        ViewInteraction materialTextView3 = onView(
                allOf(withId(android.R.id.title), withText("删除0"),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                0),
                        isDisplayed()));
        materialTextView3.perform(click());

        ViewInteraction materialButton3 = onView(
                allOf(withId(android.R.id.button1), withText("确定"),
                        childAtPosition(
                                childAtPosition(
                                        withClassName(is("android.widget.ScrollView")),
                                        0),
                                3)));
        materialButton3.perform(scrollTo(), click());

        ViewInteraction recyclerView4 = onView(
                allOf(withId(R.id.tour_records_recyclerView),
                        childAtPosition(
                                withClassName(is("android.widget.LinearLayout")),
                                0)));
        recyclerView4.perform(actionOnItemAtPosition(0, longClick()));

        ViewInteraction materialTextView4 = onView(
                allOf(withId(android.R.id.title), withText("删除0"),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                0),
                        isDisplayed()));
        materialTextView4.perform(click());

        ViewInteraction materialButton4 = onView(
                allOf(withId(android.R.id.button1), withText("确定"),
                        childAtPosition(
                                childAtPosition(
                                        withClassName(is("android.widget.ScrollView")),
                                        0),
                                3)));
        materialButton4.perform(scrollTo(), click());

        ViewInteraction materialButton5 = onView(
                allOf(withId(R.id.add_record_button), withText("添加记录"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.fragment_container),
                                        0),
                                1),
                        isDisplayed()));
        materialButton5.perform(click());

        ViewInteraction appCompatEditText = onView(
                allOf(withId(R.id.Records_Title_Text),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.main),
                                        0),
                                0)));
        appCompatEditText.perform(scrollTo(), replaceText("暨南大学"), closeSoftKeyboard());

        ViewInteraction appCompatEditText2 = onView(
                allOf(withId(R.id.editText),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.main),
                                        0),
                                7)));
        appCompatEditText2.perform(scrollTo(), replaceText("暨南大学（Jinan University）简称“暨大”，本部位于广东省广州市，学校是中央统战部、教育部、广东省人民政府共建的国家“双一流”建设高校，直属中央统战部管理；位列国家“双一流”、“211工程”、 “985工程优势学科创新平台” 、广东省高水平大学重点建设高校。"), closeSoftKeyboard());

        ViewInteraction materialButton6 = onView(
                allOf(withId(R.id.Locatebutton), withText("定位"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.main),
                                        0),
                                6)));
        materialButton6.perform(scrollTo(), click());

        ViewInteraction materialButton7 = onView(
                allOf(withId(R.id.Upload_Button), withText("上传图片"),
                        childAtPosition(
                                childAtPosition(
                                        withClassName(is("android.widget.LinearLayout")),
                                        1),
                                0)));
        materialButton7.perform(scrollTo(), click());

        ViewInteraction textView = onView(
                allOf(withId(R.id.tourViews), withText("暨南大学（Jinan Universit......"),
                        withParent(withParent(withId(R.id.tour_records_recyclerView))),
                        isDisplayed()));
        textView.check(matches(withText("暨南大学（Jinan Universit......")));

        ViewInteraction textView2 = onView(
                allOf(withId(R.id.tourLocation), withText("地址：广东省珠海市香洲区前山路206号暨南大学(珠海校区)"),
                        withParent(withParent(withId(R.id.tour_records_recyclerView))),
                        isDisplayed()));
        textView2.check(matches(withText("地址：广东省珠海市香洲区前山路206号暨南大学(珠海校区)")));

        ViewInteraction textView3 = onView(
                allOf(withId(R.id.tourlongitude), withText("经度：113.53553"),
                        withParent(withParent(withId(R.id.tour_records_recyclerView))),
                        isDisplayed()));
        textView3.check(matches(withText("经度：113.53553")));

        ViewInteraction textView4 = onView(
                allOf(withId(R.id.tourlatitude), withText("纬度：22.25242"),
                        withParent(withParent(withId(R.id.tour_records_recyclerView))),
                        isDisplayed()));
        textView4.check(matches(withText("纬度：22.25242")));

        ViewInteraction textView5 = onView(
                allOf(withId(R.id.tourTitle), withText("暨南大学"),
                        withParent(withParent(withId(R.id.tour_records_recyclerView))),
                        isDisplayed()));
        textView5.check(matches(withText("暨南大学")));

        onView(withId(R.id.tour_records_recyclerView))
                .perform(actionOnItemAtPosition(0, // 替换为目标位置
                        new ViewAction() {
                            @Override
                            public Matcher<View> getConstraints() {
                                return isAssignableFrom(ImageView.class);
                            }

                            @Override
                            public String getDescription() {
                                return "Check image in item at position 0";
                            }

                            @Override
                            public void perform(UiController uiController, View view) {
                                Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
                                ImageView imageView = view.findViewById(R.id.image_tour);
                                Bitmap actualBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                                assertTrue(areImagesSame(getCoverBitmap(R.drawable.jnu1,appContext), actualBitmap));
                            }
                        }
                ));

        ViewInteraction recyclerView5 = onView(
                allOf(withId(R.id.tour_records_recyclerView),
                        childAtPosition(
                                withClassName(is("android.widget.LinearLayout")),
                                0)));
        recyclerView5.perform(actionOnItemAtPosition(0, longClick()));

        ViewInteraction materialTextView5 = onView(
                allOf(withId(android.R.id.title), withText("查看0"),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                0),
                        isDisplayed()));
        materialTextView5.perform(click());

        ViewInteraction textView6 = onView(
                allOf(withId(R.id.ShowViews), withText("暨南大学（Jinan University）简称“暨大”，本部位于广东省广州市，学校是中央统战部、教育部、广东省人民政府共建的国家“双一流”建设高校，直属中央统战部管理；位列国家“双一流”、“211工程”、 “985工程优势学科创新平台” 、广东省高水平大学重点建设高校。"),
                        withParent(allOf(withId(R.id.main),
                                withParent(IsInstanceOf.<View>instanceOf(android.widget.ScrollView.class)))),
                        isDisplayed()));
        textView6.check(matches(withText("暨南大学（Jinan University）简称“暨大”，本部位于广东省广州市，学校是中央统战部、教育部、广东省人民政府共建的国家“双一流”建设高校，直属中央统战部管理；位列国家“双一流”、“211工程”、 “985工程优势学科创新平台” 、广东省高水平大学重点建设高校。")));

        ViewInteraction textView7 = onView(
                allOf(withId(R.id.showLocation), withText("地址：广东省珠海市香洲区前山路206号暨南大学(珠海校区)"),
                        withParent(allOf(withId(R.id.main),
                                withParent(IsInstanceOf.<View>instanceOf(android.widget.ScrollView.class)))),
                        isDisplayed()));
        textView7.check(matches(withText("地址：广东省珠海市香洲区前山路206号暨南大学(珠海校区)")));

        ViewInteraction textView8 = onView(
                allOf(withId(R.id.showlatitude), withText("纬度：22.25242"),
                        withParent(withParent(withId(R.id.main))),
                        isDisplayed()));
        textView8.check(matches(withText("纬度：22.25242")));

        ViewInteraction textView9 = onView(
                allOf(withId(R.id.showlongitude), withText("经度：113.53553"),
                        withParent(withParent(withId(R.id.main))),
                        isDisplayed()));
        textView9.check(matches(withText("经度：113.53553")));

        ViewInteraction textView10 = onView(
                allOf(withId(R.id.ShowTitle), withText("暨南大学"),
                        withParent(allOf(withId(R.id.main),
                                withParent(IsInstanceOf.<View>instanceOf(android.widget.ScrollView.class)))),
                        isDisplayed()));
        textView10.check(matches(withText("暨南大学")));

        ViewInteraction imageView2 = onView(
                allOf(withId(R.id.imageView), withContentDescription("上传图片"),
                        withParent(withParent(IsInstanceOf.<View>instanceOf(androidx.recyclerview.widget.RecyclerView.class))),
                        isDisplayed()));
        imageView2.check(matches(isDisplayed()));
    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}
